home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LT_CreateHandle.c < prev    next >
C/C++ Source or Header  |  1996-09-23  |  14KB  |  525 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. LayoutHandle * LIBENT
  15. LT_CreateHandle(REG(a0) struct Screen *Screen,REG(a1) struct TextAttr *Font)
  16. {
  17.     return(LT_CreateHandleTags(Screen,
  18.         LH_Font,Font,
  19.     TAG_DONE));
  20. }
  21.  
  22.  
  23. /*****************************************************************************/
  24.  
  25.  
  26. LayoutHandle *
  27. LT_CreateHandleTags(struct Screen *Screen,...)
  28. {
  29.     LayoutHandle    *Handle;
  30.     va_list          VarArgs;
  31.  
  32.     va_start(VarArgs,Screen);
  33.     Handle = LT_CreateHandleTagList(Screen,(struct TagItem *)VarArgs);
  34.     va_end(VarArgs);
  35.  
  36.     return(Handle);
  37. }
  38.  
  39.  
  40. /*****************************************************************************/
  41.  
  42.  
  43. /****** gtlayout.library/LT_CreateHandleTagList ******************************************
  44. *
  45. *   NAME
  46. *    LT_CreateHandleTagList -- Allocate auxilary data required by LT_New()
  47. *                              and LT_BuildA().
  48. *
  49. *   SYNOPSIS
  50. *    Handle = LT_CreateHandleTagList(Screen,Tags);
  51. *      D0                              A0    A1
  52. *
  53. *    LayoutHandle *LT_CreateHandleTagList(struct Screen *,struct TagItem *);
  54. *
  55. *    Handle = LT_CreateHandleTags(Screen,...);
  56. *
  57. *    struct LayoutHandle *LT_CreateHandleTags(struct Screen *,...);
  58. *
  59. *   FUNCTION
  60. *    Memory is allocated, tables are set up and data is collected
  61. *    on a screen a user interface is to be opened on. This
  62. *    involves calculating the screen font parameters.
  63. *
  64. *   INPUTS
  65. *    Screen - Pointer to the screen the user interface is to
  66. *        use. Passing NULL will cause the default public
  67. *        screen to be used.
  68. *
  69. *            NOTE: if NULL is passed the default public screen
  70. *                will stay locked until LT_DeleteHandle()
  71. *                is called.
  72. *
  73. *    Tags - Tag values to control certain aspects of the
  74. *        user interface created.
  75. *
  76. *
  77. *    Valid tags include:
  78. *
  79. *    LAHN_AutoActivate (BOOL) - Set to TRUE if you want the interface
  80. *        to always keep a string gadget active if possible. Hitting
  81. *        the return key will then cause the next following string
  82. *        gadget to get activated, either cycling through all the
  83. *        string gadgets available or stopping at the next string
  84. *        gadget to have the LAST_LastGadget attribute set.
  85. *        Default: TRUE
  86. *
  87. *    LAHN_RawKeyFilter (BOOL) - Discard unprocessed IDCMP_RAWKEY
  88. *        events. (V13)
  89. *        Default: TRUE
  90. *
  91. *    LAHN_UserData (APTR) - Store user specific data in the
  92. *        LayoutHandle->UserData entry.
  93. *
  94. *            NOTE: This tag requires gtlayout.library v9 and the
  95. *                corresponding entry in the LayoutHandle exists
  96. *                only under gtlayout.library v9 and up. *NEVER*
  97. *                write to this entry, use LT_SetAttributes()
  98. *                instead.
  99. *
  100. *    LAHN_LocaleHook (struct Hook *) - The hook to call when
  101. *        locale string IDs are to be mapped to strings. The
  102. *        hook function is called with the following parameters:
  103. *
  104. *        String = HookFunc(struct Hook *Hook,struct LayoutHandle *Handle,
  105. *          D0                            A0                         A2
  106. *                          LONG ID)
  107. *                               A1
  108. *
  109. *        The function is to look up the string associated with the ID
  110. *        passed in and return the string.
  111. *        Default: no locale hook
  112. *
  113. *    LAHN_TextAttr (struct TTextAttr *) - The text font to use when
  114. *        creating the gadgets and objects.
  115. *        Default: Screen->Font
  116. *
  117. *    LAHN_CloningPermitted (BOOL) - If a window will not fit onto the
  118. *        screen the user interface is intended for, the layout engine
  119. *        will scale the interface data down while stepping down in
  120. *        font size. If all this fails, the engine will open a custom
  121. *        screen for the window; this process is called "cloning".
  122. *        The LAHN_CloningPermitted tag controls whether the engine will
  123. *        actually try to open the custom screen or just return NULL
  124. *        when LT_Build fails.
  125. *        Default: TRUE
  126. *
  127. *    LAHN_EditHook (struct Hook *) - You can specify a default string
  128. *        gadget editing hook to be used for all following string
  129. *        gadgets. Your hook should obey the same rules that apply
  130. *        to hooks passed via GTST_EditHook/GTIN_EditHook.
  131. *        Default: NULL
  132. *
  133. *    LAHN_ExactClone (BOOL) - This tag works in conjunction with the
  134. *        LAHN_CloningPermitted tag. By default the layout engine will
  135. *        try to replicate only the basic characteristics of the
  136. *        screen the window was intended to open on. This may result
  137. *        in a screen which uses less colours than the original
  138. *        screen. You can force the engine to make an almost exact
  139. *        clone of the original screen by passing the LAHN_ExactClone
  140. *        tag with a value of TRUE.
  141. *        Default: FALSE
  142. *
  143. *    LAHN_MenuGlyphs (BOOL) - This tag will make the layout engine
  144. *        fill in the AmigaGlyph and CheckGlyph entries of the
  145. *        LayoutHandle if running under Kickstart 3.0 or higher.
  146. *        The corresponding images will be scaled to fit the actual
  147. *        screen aspect ratio values and can later be used for
  148. *        menu layout.
  149. *        Default: FALSE
  150. *
  151. *    LAHN_Parent (struct Window *) - You can pass a pointer to the
  152. *        parent window of the window you intend to open using
  153. *        the user interface layout engine. The new window will
  154. *        open inside the boundaries of the parent window. If the
  155. *        size does not fit, it will be opened centered over the
  156. *        parent window.
  157. *        Default: NULL
  158. *
  159. *    LAHN_BlockParent (BOOL) - This tag works in conjunction with the
  160. *        LAHN_Parent tag. If in effect, will block the parent window
  161. *        via LT_LockWindow until the new window is closed, after
  162. *        which the parent window is unlocked again.
  163. *        Default: FALSE
  164. *
  165. *    LAHN_SimpleClone (BOOL) - This tag works in conjunction with the
  166. *        LAHN_CloningPermitted tag. It will make the layout engine
  167. *        forget most information about the original screen the
  168. *        user interface was intended for. In short, it will open a
  169. *        simple default screen for the interface.
  170. *        Default: FALSE
  171. *
  172. *    LAHN_ExitFlush (BOOL) - When the LayoutHandle is finally disposed
  173. *        of with LT_DeleteHandle() all variables maintained by the
  174. *        input handling code will be flushed. For example, if you
  175. *        would use the LA_STRPTR tag for STRING_KIND objects the
  176. *        last string gadget contents would be copied into the buffer
  177. *        pointed to by LA_STRPTR. If you do not want to use this
  178. *        feature, disable it with "LAHN_ExitFlush,FALSE". (V9)
  179. *        Default: TRUE
  180. *
  181. *    LAHN_NoKeys (BOOL) - Use TRUE to tell the library not to pick
  182. *        keyboard shortcuts all on its own. This works like calling
  183. *        LT_New() for all objects with "LA_NoKey,TRUE,". (V26)
  184. *
  185. *    LAHN_PubScreen (struct Screen *) - Pointer to public screen
  186. *        window is to open on. Must be locked and open until you call
  187. *        LT_Built().
  188. *
  189. *    LAHN_PubScreenName (STRPTR) - Name of public screen to open window
  190. *        on. The library will try to lock the named screen as soon as
  191. *        you call LT_CreateHandle.
  192. *
  193. *    LAHN_PubScreenFallBack (BOOL) - If the named public screen cannot
  194. *        be found and you ask for it, the library will lock the default
  195. *        public screen (default: TRUE).
  196. *
  197. *   RESULT
  198. *    Handle - Pointer to a LayoutHandle structure.
  199. *
  200. ******************************************************************************
  201. *
  202. */
  203.  
  204. LayoutHandle * LIBENT
  205. LT_CreateHandleTagList(REG(a0) struct Screen *Screen,REG(a1) struct TagItem *TagList)
  206. {
  207.     LayoutHandle    *Handle;
  208.     struct Screen    *PubScreen;
  209.     APTR             Pool;
  210.     ULONG             WA_ScreenTag;
  211.     BOOL             UnlockThePubScreen;
  212.  
  213. #ifdef DO_PICKSHORTCUTS
  214.     ObtainSemaphore(<P_KeySemaphore);
  215.  
  216.     if(!LTP_Keys[1])
  217.     {
  218.         UBYTE mapBuffer[2 * 3];
  219.         UBYTE remapBuffer[10];
  220.         LONG i;
  221.         struct InputEvent event;
  222.  
  223.         LTP_Keys[1] = <P_Keys[0][256];
  224.  
  225.         for(i = 32 ; i < 256; i++)
  226.         {
  227.             if(i == 128)
  228.                 i = 160;
  229.  
  230.             remapBuffer[0] = i;
  231.             remapBuffer[1] = 0;
  232.  
  233.             if(MapANSI(remapBuffer,1,mapBuffer,3,NULL) == 1)
  234.             {
  235.                 if(!(mapBuffer[1] & ~QUALIFIER_SHIFT))
  236.                 {
  237.                     event.ie_NextEvent            = NULL;
  238.                     event.ie_Class                = IECLASS_RAWKEY;
  239.                     event.ie_SubClass            = 0;
  240.                     event.ie_Code                 = mapBuffer[0];
  241.                     event.ie_Qualifier            = mapBuffer[1] & ~QUALIFIER_SHIFT;
  242.                     event.ie_position.ie_addr    = NULL;
  243.  
  244.                     if(MapRawKey(&event,remapBuffer,10,NULL) == 1)
  245.                         LTP_Keys[0][i] = remapBuffer[0];
  246.  
  247.                     event.ie_NextEvent            = NULL;
  248.                     event.ie_Class                = IECLASS_RAWKEY;
  249.                     event.ie_SubClass            = 0;
  250.                     event.ie_Code                 = mapBuffer[0];
  251.                     event.ie_Qualifier            = mapBuffer[1] | QUALIFIER_SHIFT;
  252.                     event.ie_position.ie_addr    = NULL;
  253.  
  254.                     if(MapRawKey(&event,remapBuffer,10,NULL) == 1)
  255.                         LTP_Keys[1][i] = remapBuffer[0];
  256.                 }
  257.             }
  258.         }
  259.     }
  260.  
  261.     ReleaseSemaphore(<P_KeySemaphore);
  262. #endif    /* DO_PICKSHORTCUTS */
  263.  
  264.     UnlockThePubScreen = TRUE;
  265.     WA_ScreenTag = WA_CustomScreen;
  266.  
  267.     if(!Screen)
  268.     {
  269.         if(PubScreen = (struct Screen *)GetTagData(LAHN_PubScreen,NULL,TagList))
  270.         {
  271.             UnlockThePubScreen = FALSE;
  272.             WA_ScreenTag = WA_PubScreen;
  273.             Screen = PubScreen;
  274.         }
  275.         else
  276.         {
  277.             STRPTR PubName;
  278.  
  279.             if(PubName = (STRPTR)GetTagData(LAHN_PubScreenName,NULL,TagList))
  280.             {
  281.                 if(!(PubScreen = LockPubScreen(PubName)))
  282.                 {
  283.                     if(!GetTagData(LAHN_PubScreenFallBack,TRUE,TagList))
  284.                         return(NULL);
  285.                 }
  286.             }
  287.             else
  288.                 PubScreen = NULL;
  289.  
  290.             if(!PubScreen)
  291.                 PubScreen = LockPubScreen(NULL);
  292.  
  293.             if(!PubScreen)
  294.                 return(NULL);
  295.             else
  296.             {
  297.                 Screen = PubScreen;
  298.                 WA_ScreenTag = WA_PubScreen;
  299.             }
  300.         }
  301.     }
  302.     else
  303.     {
  304.         PubScreen = NULL;
  305.         UnlockThePubScreen = FALSE;
  306.     }
  307.  
  308.     if(Pool = AsmCreatePool(MEMF_PUBLIC | MEMF_ANY | MEMF_CLEAR,1024,1024,SysBase))
  309.     {
  310.         if(Handle = AsmAllocPooled(Pool,sizeof(LayoutHandle),SysBase))
  311.         {
  312.             struct TagItem    *List,
  313.                             *Entry;
  314.             BOOL             MenuGlyphs;
  315.  
  316.             MenuGlyphs = FALSE;
  317.  
  318.             Handle->Pool                = Pool;
  319.             Handle->Screen                = Screen;
  320.             Handle->PubScreen            = PubScreen;
  321.             Handle->PointBack            = Handle;
  322.             Handle->GroupID                = PHANTOM_GROUP_ID;
  323.  
  324.             Handle->WA_ScreenTag        = WA_ScreenTag;
  325.             Handle->UnlockPubScreen        = UnlockThePubScreen;
  326.  
  327.             Handle->ExitFlush            = TRUE;
  328.             Handle->RawKeyFilter        = TRUE;
  329. #ifdef DO_CLONING
  330.             Handle->CloningPermitted    = TRUE;
  331. #endif
  332.             Handle->StandardEditHook    = &Handle->DefaultEditHook;
  333.  
  334.             List = TagList;
  335.  
  336.             while(Entry = NextTagItem(&List))
  337.             {
  338.                 switch(Entry->ti_Tag)
  339.                 {
  340.                     case LH_Font:
  341.  
  342.                         Handle->InitialTextAttr = (struct TextAttr *)Entry->ti_Data;
  343.                         break;
  344.  
  345.                     case LH_MenuGlyphs:
  346.  
  347.                         MenuGlyphs = Entry->ti_Data;
  348.                         break;
  349.  
  350.                     case LH_Parent:
  351.  
  352.                         Handle->Parent = (struct Window *)Entry->ti_Data;
  353.                         break;
  354.  
  355.                     case LH_BlockParent:
  356.  
  357.                         Handle->BlockParent = Entry->ti_Data;
  358.                         break;
  359.  
  360.                     case LH_ExitFlush:
  361.  
  362.                         Handle->ExitFlush = Entry->ti_Data;
  363.                         break;
  364.  
  365.                     case LH_UserData:
  366.  
  367.                         Handle->UserData = (APTR)Entry->ti_Data;
  368.                         break;
  369.  
  370.                     case LH_RawKeyFilter:
  371.  
  372.                         Handle->RawKeyFilter = Entry->ti_Data;
  373.                         break;
  374.  
  375.                     case LH_AutoActivate:
  376.  
  377.                         Handle->AutoActivate = Entry->ti_Data;
  378.                         break;
  379.  
  380.                     case LH_LocaleHook:
  381.  
  382.                         Handle->LocaleHook = (struct Hook *)Entry->ti_Data;
  383.                         break;
  384.  
  385.                     case LH_EditHook:
  386.  
  387.                         Handle->StandardEditHook = (struct Hook *)Entry->ti_Data;
  388.                         break;
  389.  
  390.                     case LAHN_NoKeys:
  391.  
  392.                         Handle->NoKeys = Entry->ti_Data;
  393.                         break;
  394. #ifdef DO_CLONING
  395.                     case LH_CloningPermitted:
  396.  
  397.                         Handle->CloningPermitted = Entry->ti_Data;
  398.                         break;
  399.  
  400.                     case LH_SimpleClone:
  401.  
  402.                         if(Handle->SimpleClone = Entry->ti_Data)
  403.                             Handle->ExactClone = FALSE;
  404.  
  405.                         break;
  406.  
  407.                     case LH_ExactClone:
  408.  
  409.                         if(Handle->ExactClone = Entry->ti_Data)
  410.                             Handle->SimpleClone = FALSE;
  411.  
  412.                         break;
  413. #endif
  414.                 }
  415.             }
  416.  
  417. #ifdef DO_PASSWORD_KIND
  418.             Handle->PasswordEditHook.h_Entry    = (HOOKFUNC)LTP_PasswordEditRoutine;
  419.             Handle->PasswordEditHook.h_Data        = Handle;
  420. #endif
  421.  
  422.             Handle->DefaultEditHook.h_Entry    = (HOOKFUNC)LTP_DefaultEditRoutine;
  423.             Handle->DefaultEditHook.h_Data    = Handle;
  424.  
  425.             Handle->BackfillHook.h_Entry    = (HOOKFUNC)LTP_BackfillRoutine;
  426.             Handle->BackfillHook.h_Data     = Handle;
  427.  
  428. #ifdef DO_HEXHOOK
  429.             Handle->HexEditHook.h_Entry     = (HOOKFUNC)LTP_HexEditRoutine;
  430.             Handle->HexEditHook.h_Data        = Handle;
  431. #endif
  432.  
  433.             if(Handle->DrawInfo = GetScreenDrawInfo(Screen))
  434.             {
  435.                 LONG i;
  436.  
  437.                 Handle->TextPen            = Handle->DrawInfo->dri_Pens[TEXTPEN];
  438.                 Handle->BackgroundPen    = Handle->DrawInfo->dri_Pens[BACKGROUNDPEN];
  439.                 Handle->ShinePen        = Handle->DrawInfo->dri_Pens[SHINEPEN];
  440.                 Handle->ShadowPen        = Handle->DrawInfo->dri_Pens[SHADOWPEN];
  441.                 Handle->AspectX            = Handle->DrawInfo->dri_Resolution.X;
  442.                 Handle->AspectY            = Handle->DrawInfo->dri_Resolution.Y;
  443.  
  444.                 if(Handle->BackgroundPen)
  445.                     InitRastPort(&Handle->BackfillRastPort);
  446.  
  447.                 if(V39 && MenuGlyphs)
  448.                 {
  449.                     LONG i,Size,Width,Height = Handle->DrawInfo->dri_Font->tf_Baseline + 2;
  450.                     struct Image **Glyphs[2];
  451.  
  452.                     Glyphs[0] = &Handle->AmigaGlyph;
  453.                     Glyphs[1] = &Handle->CheckGlyph;
  454.  
  455.                     if(Screen->Flags & SCREENHIRES)
  456.                         Size = SYSISIZE_MEDRES;
  457.                     else
  458.                         Size = SYSISIZE_LOWRES;
  459.  
  460.                     for(i = 0 ; i < 2 ; i++)
  461.                     {
  462.                         if(i)
  463.                             Width = (Height * Handle->AspectY) / Handle->AspectX;
  464.                         else
  465.                             Width = (Height * 3 * Handle->AspectY) / (2 * Handle->AspectX);
  466.  
  467.                         if(!(*Glyphs[i] = NewObject(NULL,SYSICLASS,
  468.                             SYSIA_DrawInfo, Handle->DrawInfo,
  469.                             SYSIA_Size,        Size,
  470.                             SYSIA_Which,    i ? MENUCHECK : AMIGAKEY,
  471.                             IA_Width,        Width,
  472.                             IA_Height,        Height,
  473.                         TAG_DONE)))
  474.                         {
  475.                             DisposeObject(*Glyphs[0]);
  476.                             *Glyphs[0] = NULL;
  477.  
  478.                             break;
  479.                         }
  480.                     }
  481.                 }
  482.  
  483.                 for(i = 0 ; i < Handle->DrawInfo->dri_NumPens ; i++)
  484.                 {
  485.                     if(Handle->DrawInfo->dri_Pens[i] > Handle->MaxPen)
  486.                         Handle->MaxPen = Handle->DrawInfo->dri_Pens[i];
  487.                 }
  488.  
  489.                 if(Handle->VisualInfo = GetVisualInfoA(Screen,NULL))
  490.                 {
  491.                     InitRastPort(&Handle->RPort);
  492.  
  493.                     if(LTP_GlyphSetup(Handle,Handle->InitialTextAttr))
  494.                     {
  495. #ifdef DO_PICKSHORTCUTS
  496.                         memset(Handle->Keys,TRUE,256);
  497.  
  498.                         for(i = 0 ; i < 256 ; i++)
  499.                         {
  500.                             if(i != 32 && i != 160)
  501.                             {
  502.                                 if(LTP_Keys[0][i])
  503.                                     Handle->Keys[LTP_Keys[0][i]] = FALSE;
  504.  
  505.                                 if(LTP_Keys[1][i])
  506.                                     Handle->Keys[LTP_Keys[1][i]] = FALSE;
  507.                             }
  508.                         }
  509. #endif
  510.                         return(Handle);
  511.                     }
  512.                 }
  513.             }
  514.  
  515.             LT_DeleteHandle(Handle);
  516.         }
  517.         else
  518.             AsmDeletePool(Pool,SysBase);
  519.     }
  520.     else
  521.         UnlockPubScreen(NULL,PubScreen);
  522.  
  523.     return(NULL);
  524. }
  525.